<script setup>
import {
  Back
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { merchantPostService } from '@/api/merchant';

const router = useRouter();

// 表单数据
const formRef = ref()
const formData = ref({
  name: '',
  contactPhone: ''
})
const formFileData = ref({
  avatar: null
})

// 表单验证规则
const rules = ref({
  name: [
    { required: true, message: '请输入店铺名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在2到20个字符', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
})

// 头像上传处理
const avatarUrl = ref('');
const onAvatarFileChange = (file) => {
  formFileData.value.avatar = file.raw;
  avatarUrl.value = URL.createObjectURL(file.raw);
}

// 提交处理
const submitForm = async () => {
  await formRef.value.validate()
  await merchantPostService(formData.value, formFileData.value);
}
</script>

<template>
  <el-container class="merchant-apply-container">
    <el-header>
      <el-icon @click="router.back()"><Back /></el-icon>
    </el-header>
    <el-main class="form-wrapper">
      <h1 class="title">申请开店</h1>
      <el-form 
        ref="formRef"
        :model="formData" 
        :rules="rules"
        label-position="top"
        class="apply-form"
      >
        <!-- 头像上传 -->
        <el-form-item label="店铺头像">
          <el-upload
            class="avatar-uploader"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="onAvatarFileChange"
            accept="image/*"
          >
            <img v-if="avatarUrl" :src="avatarUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <i-ep-plus />
            </el-icon>
          </el-upload>
        </el-form-item>

        <!-- 店铺名称 -->
        <el-form-item label="店铺名称" prop="name">
          <el-input 
            v-model="formData.name"
            placeholder="请输入店铺名称"
            clearable
          />
        </el-form-item>

        <!-- 联系方式 -->
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input
            v-model="formData.contactPhone"
            placeholder="请输入联系电话"
            maxlength="11"
            clearable
          />
        </el-form-item>

        <!-- 提交按钮 -->
        <el-form-item>
          <el-button 
            type="primary" 
            class="submit-btn"
            @click="submitForm"
          >
            立即申请
          </el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<style lang="scss" scoped>
.merchant-apply-container {
  min-height: 100vh;
  box-sizing: border-box;
  background: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-header {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    height: auto;
    padding: 15px 8px;
    color: black;
    font-size: 25px;
    cursor: pointer;
  }

  .form-wrapper {
    background: white;
    border-radius: 12px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
    padding: 40px;
    width: 100%;
    max-width: 600px;
    margin-bottom: 50px;

    .title {
      text-align: center;
      color: #303133;
      margin-bottom: 30px;
      font-size: 24px;
      font-weight: 500;
    }

    .apply-form {
      :deep(.el-form-item__label) {
        font-weight: 500;
        color: #606266;
      }

      .avatar-uploader {
        :deep(.el-upload) {
          border: 1px dashed var(--el-border-color);
          border-radius: 50%;
          cursor: pointer;
          position: relative;
          overflow: hidden;
          transition: var(--el-transition-duration-fast);
          width: 120px;
          height: 120px;

          &:hover {
            border-color: var(--el-color-primary);
          }
        }

        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 120px;
          height: 120px;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .avatar {
          width: 120px;
          height: 120px;
          display: block;
          object-fit: cover;
        }
      }

      .submit-btn {
        width: 100%;
        height: 44px;
        font-size: 16px;
        margin-top: 20px;
      }
    }
  }
}
</style>